﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GrayParrot.Web.Controls
{
    /// <summary>
    /// Options Comparer Base class...
    /// </summary>
    public abstract class ComparerBase<T> : IEqualityComparer<T>
    {
        internal abstract bool AllowedTypes(Type type);
        internal abstract bool CheckDuplicates(Type type);

        public bool Equals(T x, T y)
        {
            if (!AllowedTypes(x.GetType())) throw new Exception(string.Format("Option {0} not allowed ", x.GetType()));
            if (!AllowedTypes(y.GetType())) throw new Exception(string.Format("Option {0} not allowed", y.GetType()));

           // return x.GetType().Equals(y.GetType()) && (x.GetType().Equals(typeof(LengthMenu)));
            return x.GetType().Equals(y.GetType()) && CheckDuplicates(x.GetType()); //(x.GetType().Equals(typeof(LengthMenu)));
        }

        /// <summary>
        /// Returns a hash code for this instance.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <returns>
        /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 
        /// </returns>
        public int GetHashCode(T obj)
        {
            return obj.GetType().GetHashCode();
        }
    }
}
